package com.ken.spring.beans;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

@Repository
public class BookShopDao {
    @Autowired
    JdbcTemplate jdbcTemplate;


    /**
     * 根据id 查找书本的 价格
     */
    public Float findBookPriceById(Integer id) {
        String sql = "SELECT  price FROM book_shop where id= ?";

        return jdbcTemplate.queryForObject(sql, Float.class, id);
    }


    /**
     * 更新书本的库存
     */
    public void updateBookStockById(Integer id) {
//        查询书的库存
        String sql = "SELECT  stock FROM book_shop where id= ?";

        int stocks = jdbcTemplate.queryForObject(sql, Integer.class, id);

//更新库存
        String sql2 = "UPDATE book_shop set stock = ?-1 where id = ? ";
        int i = jdbcTemplate.update(sql2, stocks, id);

        System.out.println("更新书本的库存->" + i);
    }



}
